package com.aaa.fees.service.ipml;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.fees.dao.CareOederItemDao;
import com.aaa.fees.dao.CareOrderDao;
import com.aaa.fees.dao.ChargeItemDao;
import com.aaa.fees.dao.SendMedicineDao;
import com.aaa.fees.service.SendMedicineService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SendMedicineServiceImpl implements SendMedicineService {
    @Autowired
    SendMedicineDao sendMedicineDao;
    @Autowired
    CareOrderDao careOrderDao;
    @Autowired
    ChargeItemDao chargeItemDao;
    @Autowired
    CareOederItemDao careOederItemDao;

    @Override
    public Result<Map<String,Object>> getSendMeId(String regId) {
        //根据前端穿过来的reg_id查询病历表信息
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id", regId);
        CareHistory careHistory = sendMedicineDao.selectOne(wrapper);
        if (careHistory == null) {
            return new Result<>(5000, "请输入已支付的挂号单", null);
        }
        //获取到ch_id查询药用处方表信息
        String chId = careHistory.getChId();
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
        List<Map<String, Object>> list = new ArrayList<>();
        if (careOrders.size() > 0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("co_id", careOrder.getCoId());
                wrapper2.eq("status", 1);
                List<CareOrderItem> careOrderItems = careOederItemDao.selectList(wrapper2);
                //计算订单总金额
                if (careOrderItems.size() != 0) {
                    Double sum = careOrderItems.stream().map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map.put("careOrderItems", careOrderItems);
                    map.put("sum", sum);
                    list.add(map);
                }
            }

        }
        if (list.isEmpty()) {
            return new Result<>(5000, "请输入已支付的订单ID", null);
        } else {
            //把数据存放在map中返回
            Map<String, Object> maps = new HashMap<>();
            maps.put("careHistory", careHistory);
            maps.put("list", list);
            return new Result<>(200, "查询数据成功", maps);
        }
    }
       //  发药
          @Override
          public Result handleDoMedicine(String[] itemIds) {
            //修改收费表查看详情状态为已支付    //修改当前页面状态为已支付
            for (int i = 0; i <itemIds.length ; i++) {
                chargeItemDao.updateStatus(itemIds[i]);
                careOederItemDao.updateStatus(itemIds[i]);
            }
            for (int i = 0; i <itemIds.length; i++) {
                QueryWrapper<CareOrderItem> wrapper = new QueryWrapper<>();
                wrapper.eq("item_id",itemIds[i]);
                CareOrderItem careOrderItem = careOederItemDao.selectOne(wrapper);
                BigDecimal num = careOrderItem.getNum();
                String itemName = careOrderItem.getItemName();
                // 库存接口
                //disPenSingFeign.medicinesInventory(String.valueOf(num), itemName);
            }
            return new Result(2000,"发药成功",true);
        }
}
